'use strict';

const base_RESTful = require('../core/base_RESTful.js');
const { config_cookies } = require('../config/config.js');
const jwt = require('jsonwebtoken');
const ms = require('ms');

exports.getList = async ctx => {
    const _this = new base_RESTful(ctx);
    const rq_data = ctx.request.body;
    const service = ctx.service;
    // const token = ctx.get("token");
    let result = null ;
    
    // const {
    //   id 
    // } = jwt.decode(token);
    // id == "" ?id = null:"";
  
    // if (!id) {
    //   _this.error({
    //     code: '5000',
    //     msg: '没有此用户',
    //     data: [],
    //   });
    // }
    
    try{
      console.log("try_____________________")
      result = await service.work.findWork(rq_data);
    //  console.log({results,rowData})
    }catch(e){
      console.log("resultsfdsfdsfsdfsdsfsdfsd")
      console.log(e);
    }
  

    console.log("work_detail+++++++++++++++")
    console.log(result)

    if (result) {
      ctx.body = {
        code: 2000,
        msg: '获取信息成功',
        data: result
      };
    } else {
      console.log("error")
      _this.error({
        code:5000,
        msg:"获取数据失败",
        data:[]
      });
    }

  };

  //  id: data.id,
  // work: data.work,
  exports.UspdateWork = async ctx =>{
    const _this = new base_RESTful(ctx);
    const rq_data = ctx.request.body;
    const service = ctx.service;
    const token = ctx.get("token");
    let result = null;
    try{
      console.log("try_____________________")
     result = await service.work.updateWork(rq_data);
    }catch(e){
      console.log(e);
    }


    if (result) {
      ctx.body = {
        code: 2000,
        msg: '修改成功',
        data: result
      };
    } else {
      console.log("error")
      _this.error({
        code:5000,
        msg:"修改失败",
        data:[]
      });
    }
  }


  exports.CreateWork = async ctx =>{
    const _this = new base_RESTful(ctx);
    const rq_data = ctx.request.body;
    const service = ctx.service;
    const token = ctx.get("token");
    let result = null;
    try{
      console.log("try_____________________")
     result = await service.work.CreateWork(rq_data);
     console.log("result")
     console.log(result)
    }catch(e){
      console.log(e);
    }

    if (result) {
      ctx.body = {
        code: 2000,
        msg: '添加成功',
        data: result
      };
    } else {
      console.log("error")
      _this.error({
        code:5000,
        msg:"添加失败",
        data:[]
      });
    }

  }

  exports.DelWork = async ctx =>{
    const _this = new base_RESTful(ctx);
    const rq_data = ctx.request.body;
    const service = ctx.service;
    const token = ctx.get("token");
    let result = null;
    try{
      console.log("try_____________________")
     result = await service.work.DelWork(rq_data);
     console.log("result")
     console.log(result)
    }catch(e){
      console.log(e);
    }

    if (result) {
      ctx.body = {
        code: 2000,
        msg: '删除成功',
        data: result
      };
    } else {
      console.log("error")
      _this.error({
        code:5000,
        msg:"删除失败",
        data:[]
      });
    }

  }


  exports.getDetailWorl = async ctx=>{
    const _this = new base_RESTful(ctx);
    const rq_data = ctx.request.body;
    const service = ctx.service;
    const token = ctx.get("token");
    let result = null;
    try{
      console.log("try_____________________")
     result = await service.work.getDetailWork(rq_data);
     console.log("result")
     console.log(result)
    }catch(e){
      console.log(e);
    }

    if (result) {
      ctx.body = {
        code: 2000,
        msg: '获取成功',
        data: result
      };
    } else {
      console.log("error")
      _this.error({
        code:5000,
        msg:"获取失败",
        data:[]
      });
    }
  }